package com.tevintao.marketingsystem.provider;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;

import com.tevintao.marketingsystem.log.LogManager;

import java.io.File;


public class LastUpdateDB {

    @SuppressLint("SdCardPath")
    public static final String SQL_NAME = "/mnt/sdcard/ms/chat.sqlite";//数据库名称。
    public static final String MAIN_DATA_TABLE_NAME = "chat";//表名。

    public static String createContactsDb = "create table if not exists " + MAIN_DATA_TABLE_NAME + "( talker varchar(50), createTime INTEGER ); ";


    public static final long GET_LAST_TIME_FAIL = -1;

    private static LastUpdateDB ourInstance = new LastUpdateDB();

    public static LastUpdateDB getInstance() {
        return ourInstance;
    }

    private SQLiteDatabase mDatabase;


    private LastUpdateDB() {
    }

    public SQLiteDatabase getDb() {
        if (null == mDatabase) {
            File dbFile = new File(SQL_NAME);
            if (dbFile.exists()) {
                mDatabase = SQLiteDatabase.openOrCreateDatabase(dbFile, null);
            } else if (!dbFile.exists()) {
                try {
                    mDatabase = SQLiteDatabase.openOrCreateDatabase(dbFile, null);
                    LogManager.getInstance().logError("mDatabase = " + mDatabase);
                    mDatabase.execSQL(createContactsDb);
                } catch (SQLException e) {
                    e.printStackTrace();
                    LogManager.getInstance().logError(" SQLException " + e);
                }
            }
        }
        return mDatabase;
    }

    public Cursor getLastTimeCursor(String field_talker) {
        Cursor cursor = getDb().query(MAIN_DATA_TABLE_NAME, null, "talker = ? ", new String[]{field_talker}, null, null, null);
        if (cursor.getCount() <= 0) {
            ContentValues cv = new ContentValues();
            cv.put("talker", field_talker);
            cv.put("createTime", System.currentTimeMillis());
            long insertNum = getDb().insert(MAIN_DATA_TABLE_NAME, null, cv);
            LogManager.getInstance().logInfo("插入 field_talker = " + field_talker + " num = " + insertNum);
        }
        return cursor;
    }

//    public long getLastTime(String field_talker) {
//        SQLiteDatabase db = getDb();
//        Cursor cursor = db.query(MAIN_DATA_TABLE_NAME, null, "talker = ? ", new String[]{field_talker}, null, null, null);
//        long lastTime;
//        if (cursor.moveToNext()) {
//            lastTime = cursor.getLong(cursor.getColumnIndex("createTime"));
//        } else {
//            ContentValues cv = new ContentValues();
//            cv.put("talker", field_talker);
//            cv.put("createTime", System.currentTimeMillis());
//            long insertNum = db.insert(MAIN_DATA_TABLE_NAME, null, cv);
//            LogManager.getInstance().logInfo("插入 field_talker = " + field_talker + " num = " + insertNum);
//            return GET_LAST_TIME_FAIL;
//        }
//        cursor.close();
//        return lastTime;
//    }

    public int updateLastTime(ContentValues values) {
        String talker = values.getAsString("talker");
        return getDb().update(MAIN_DATA_TABLE_NAME, values, "talker=?", new String[]{talker});
    }
//
//    public boolean updateLastTime(String field_talker, long time) {
//        SQLiteDatabase db = getDb();
//        ContentValues cv = new ContentValues();
//        cv.put("talker", field_talker);
//        cv.put("createTime", time);
//        return db.update(MAIN_DATA_TABLE_NAME, cv, "talker=?", new String[]{field_talker}) > 0;
//    }

    public static long getChatLastTime(Context context, String talker) {
        Uri uri = Uri.parse("content://chat_msg");
        Cursor cursor = context.getContentResolver().query(uri, null, talker, null, null);
        long lastTime = GET_LAST_TIME_FAIL;
        if (cursor != null && cursor.moveToNext()) {
            lastTime = cursor.getLong(cursor.getColumnIndex("createTime"));
            LogManager.getInstance().logInfo("lastTime = " + lastTime);
            cursor.close();
        }
        return lastTime;
    }

    public static boolean updateChatLastTime(Context context, String talker, long time) {
        Uri updateUri = Uri.parse("content://chat_msg");
        ContentValues cv = new ContentValues();
        cv.put("talker", talker);
        cv.put("createTime", time);
        int updateNum = context.getContentResolver().update(updateUri, cv, null, null);
        LogManager.getInstance().logInfo("updateNum = " + updateNum);
        return updateNum > 0;
    }

}